import React from 'react';
import { Breadcrumb, Button, Form, Input, Message } from 'antd';
const FormItem = Form.Item;

let AccountInfo = React.createClass({

  contextTypes: {
    router: React.PropTypes.object
  },


  getInitialState() {
    return {
      loading: false,
    }
  },

  handleSubmit(e) {
    e.preventDefault();

    let params = this.props.form.getFieldsValue();
    this.props.form.validateFields((errors, values) => {
      if (!!errors) {
        console.log('Errors in form!!!'); return;
      }
      this.setState({
        loading: true,
      });
      Utils.sendAjax({
        url: '/user/security/modifyPwd',
        data: JSON.stringify(params),
        callback: (result) => {
          if (result.code === '200') {
            Message.success(result.message);
            this.handleOk();
            this.context.router.push('/');
          } else {
            this.setState({
              loading: false,
            });
            Message.error(result.message, 10);
          }
        }
      });
    });
  },
  handleOk() {
    Utils.sendAjax({
      url: '/funder/logout',
      callback: (result)=> {
        if (result.code == 200) {
          localStorage.funderUserName = '';
          localStorage.funderUserName = '';
        }
      }
    })
  },

  checkPass2(rule, value, callback) {
    const form = this.props.form;
    if (value && value !== form.getFieldValue('newPwd')) {
      callback('两次输入密码不一致！');
    } else {
      callback();
    }
  },


  render() {
    const { getFieldProps } = this.props.form;
    const formItemLayout = {
      labelCol: {span: 4},
      wrapperCol: {span: 6},
    };
    const oldPwd = getFieldProps('oldPwd', {
      rules: [
        { required: true, whitespace: true, message: '请填写旧密码' },
      ]
    });

    const newPwd = getFieldProps('newPwd', {
      rules: [
        { required: true, whitespace: true, message: '请填写新密码' },
        { validator: Utils.password },
      ],
    });
    const confirmPwd = getFieldProps('confirmPwd', {
      rules: [{
        required: true,
        whitespace: true,
        message: '请再次输入密码',
      }, {
        validator: this.checkPass2,
      }],
    });
    return (
      <div className="inner-content">

        <Form horizontal form={this.props.form} >
          <FormItem
            {...formItemLayout}
            label="旧密码">
            <Input type="password" {...oldPwd} />
          </FormItem>
          <FormItem
            {...formItemLayout}
            label="新密码">
            <Input type="password" {...newPwd} />
          </FormItem>
          <FormItem
            {...formItemLayout}
            label="确认新密码">
            <Input type="password" {...confirmPwd} />
          </FormItem>
          <FormItem wrapperCol={{ span: 10, offset: 4 }} style={{ marginTop: 24 }}>
            <Button type="primary" loading={this.state.loading} onClick={this.handleSubmit}>保存</Button>
          </FormItem>
        </Form>
      </div>
    );
  }
});
AccountInfo = Form.create()(AccountInfo)
export default AccountInfo